<?php
	require_once __DIR__."/../Models/text.php";
	
	use Symfony\Component\HttpFoundation\Request;
	use Symfony\Component\HttpFoundation\Response;

	$text_model = new Models\Text($app);
	$texts = $app['controllers_factory'];


	$texts->get('/{name}', function($name) use ($app, $text_model) {
		
		$res = $text_model->getOne($name);
		$user_var = $app['session']->get('user_var');
		if(!$res){
			$res['text'] = "text";
			$res['title'] = "title";
			$res['description'] = "description";
		}
			$res['text'] = htmlspecialchars_decode( $res["text"] );
			$res['title'] = htmlspecialchars_decode( $res["title"] );
			$res['description'] = htmlspecialchars_decode( $res["description"] );

		return $app['twig']->render('editor.html', array('text'=>$res, 'name'=>$name, 'user_var' => $user_var) );			
	});

	$texts->post('/{name}', function($name, Request $request) use ($app, $text_model) {
		$title = $request->get('title');
		$text = $request->get('text');
		$description = $request->get('description');
		$project = $request->get('project');
		$oper = $request->get('oper');


		if($oper == 'edit'){
			$data = array(
					"title" => $title,
					"text" => $text,
					"name" => $name,
					"project" => $project,
					"description" => $description,
				);
			
			$text_model->update($data);
		}

		return '';
	});	

	return $texts; 
?>